package com.hsy.bibc.modules.msg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPPicassoUtil;
import com.hsy.bibc.common.Consts;
import com.hsy.bibc.R;
import com.hsy.bibc.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.bibc.modules.msg.gsondata.MsgDetailData;
import com.hsy.bibc.modules.view.ScrollGridView;
import com.hsy.bibc.utils.CommonUtils;
import com.hsy.bibc.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

public class ActivityMsgDetail extends CommonBaseActivityWithSimpleActionBar {

    private List<String> listData = new ArrayList<>();
    private LayoutInflater inflater;
    private ScrollGridView grid_view;
    private AdapterGrid adapterMyThing;
    private String orderId = null;

    private TextView create_datetime_str, view_num, name, content;

    private DPHttpUtil.HttpCallback httpCallBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                CommonUtils.hideProgressDialog();
                MsgDetailData.Result result = DPGsonUtil.fromJson(response, MsgDetailData.Result.class);
                showUI(result);
            }


        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }

        @Override
        public void onMsgError(int what, int type, String msg) {
            CommonUtils.hideProgressDialog();
        }
    };
    private MsgDetailData.Result mResult;

    private void showUI(MsgDetailData.Result result) {
        this.mResult = result;

        listData = result.getImgs();

        create_datetime_str.setText(result.getCreate_datetime_str());
        name.setText(result.getName());
        content.setText(result.getContent());

        adapterMyThing = new AdapterGrid();
        grid_view.setAdapter(adapterMyThing);
        getContentView().setVisibility(View.VISIBLE);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_news);
        setTitle(R.string.title_msg_detail);
        setTitleTextColor(R.color.tab_text_color_normal);
        getContentView().setVisibility(View.GONE);


        inflater = LayoutInflater.from(this);
        grid_view = F(R.id.grid_view);
        create_datetime_str = F(R.id.create_datetime_str);
        view_num = F(R.id.view_num);
        name = F(R.id.name);
        content = F(R.id.content);

        grid_view.setFocusable(false);

        Intent intent = getIntent();
        orderId = intent.getStringExtra("id");
        getHttp();
    }

    private void getHttp() {
        if (!isEmpty(orderId)) {
            CommonUtils.showProgressDialog(this);
            HttpUtil.httpGet(ActivityMsgDetail.this, 0, Consts.API_GET_NEWS_ZX_DETAIL + "?" + "id=" + orderId, httpCallBack);
        }
    }


    private class AdapterGrid extends BaseAdapter {

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            view = inflater.inflate(R.layout.item_grid_view, null);

            ImageView image = (ImageView) view.findViewById(R.id.image);
            DPPicassoUtil.with(ActivityMsgDetail.this).load(listData.get(i)).into(image);

            return view;
        }
    }

}
